package com.szsfm.vehiclemanagement.util;

/**
 * Created by zhoubigbo on 2017/11/24.
 */
public class ByteUtil {

    public static byte int3(int x) {
        return (byte) (x >> 24);
    }

    public static byte int2(int x) {
        return (byte) (x >> 16);
    }

    public static byte int1(int x) {
        return (byte) (x >> 8);
    }

    public static byte int0(int x) {
        return (byte) (x);
    }

    public static byte[] int2Bytes(int len){
        byte[] buf = {int0(len),int1(len),int2(len),int3(len)};
        return buf;
    }

    public static int getCount(byte b0, byte b1, byte b2, byte b3) {
        return changNum(b0) + (changNum(b1) << 8) + (changNum(b2) << 16) + (changNum(b3) << 23);
    }

    // 将符号位的byte转化为无符号位的byte
    public static int changNum(byte intValue) {
        int value = 0;
        int temp = intValue % 256;
        if (intValue < 0) {
            value = (int) (temp >= -128 ? 256 + temp : temp);
        } else {
            value = (int) (temp > 127 ? temp - 256 : temp);
        }

        return value;
    }



    public static int[] unsign2int(byte[] temp) {

        int[] ret = new int[temp.length];
        for(int i=0;i<temp.length;i++){
            ret[i] =  temp[i]&0xff;
        }

        return ret;
    }

    public static byte[] copy(byte[] src){
        int len = src.length;
        byte[] dst = new byte[len];
        System.arraycopy(src,0,dst,0,len);
        return dst;
    }

    public static byte[] mergeByte(byte... bs) {
        byte[] totle = new byte[bs.length];
        for (int i = 0; i < bs.length; i++) {
            totle[i] = bs[i];
        }
        return totle;
    }

    public static byte[] mergeByte(byte[]... bs) {
        int len = 0;
        for (byte[] b : bs) {
            len += b.length;
        }
        byte[] totle = new byte[len];
        int destPos = 0;
        for (byte[] b : bs) {
            System.arraycopy(b, 0, totle, destPos, b.length);
            destPos += b.length;
        }
        return totle;
    }
}
